// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import zombie.core.PerformanceSettings;

public final class FPSTracking {
    private final double[] lastFPS = new double[20];
    private int lastFPSCount = 0;
    private long timeAtLastUpdate;
    private final long[] last10 = new long[10];
    private int last10index = 0;

    public void init() {
        for (int _int = 0; _int < 20; _int++) {
            this.lastFPS[_int] = (double)PerformanceSettings.getLockFPS();
        }

        this.timeAtLastUpdate = System.nanoTime();
    }

    public long frameStep() {
        long long0 = System.nanoTime();
        long long1 = long0 - this.timeAtLastUpdate;
        if (long1 > 0L) {
            float _float = 0.0F;
            double double0 = (double)long1 / 1.0E9;
            double double1 = 1.0 / double0;
            this.lastFPS[this.lastFPSCount] = double1;
            this.lastFPSCount++;
            if (this.lastFPSCount >= 5) {
                this.lastFPSCount = 0;
            }

            for (int _int = 0; _int < 5; _int++) {
                _float = (float)((double)_float + this.lastFPS[_int]);
            }

            _float /= 5.0F;
            zombie.GameWindow.averageFPS = _float;
            zombie.GameTime.instance.FPSMultiplier = (float)(60.0 / double1);
            if (zombie.GameTime.instance.FPSMultiplier > 5.0F) {
                zombie.GameTime.instance.FPSMultiplier = 5.0F;
            }
        }

        this.timeAtLastUpdate = long0;
        this.updateFPS(long1);
        return long1;
    }

    public void updateFPS(long timeDiff) {
        this.last10[this.last10index++] = timeDiff;
        if (this.last10index >= this.last10.length) {
            this.last10index = 0;
        }

        float float0 = 11110.0F;
        float float1 = -11110.0F;

        for (long _long : this.last10) {
            if (_long != 0L) {
                if ((float)_long < float0) {
                    float0 = (float)_long;
                }

                if ((float)_long > float1) {
                    float1 = (float)_long;
                }
            }
        }
    }
}
